{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div style=\"color:#777777;background-color:#ffffff;font-size:12px;text-align:right;\">\n",
    "\tprepared by Abuzer Yakaryilmaz (QuSoft@Riga) | November 07, 2018\n",
    "</div>\n",
    "<table><tr><td><i> I have some macros here. If there is a problem with displaying mathematical formulas, please run me to load these macros.</i></td></td></table>\n",
    "$ \\newcommand{\\bra}[1]{\\langle #1|} $\n",
    "$ \\newcommand{\\ket}[1]{|#1\\rangle} $\n",
    "$ \\newcommand{\\braket}[2]{\\langle #1|#2\\rangle} $\n",
    "$ \\newcommand{\\inner}[2]{\\langle #1,#2\\rangle} $\n",
    "$ \\newcommand{\\biginner}[2]{\\left\\langle #1,#2\\right\\rangle} $\n",
    "$ \\newcommand{\\mymatrix}[2]{\\left( \\begin{array}{#1} #2\\end{array} \\right)} $\n",
    "$ \\newcommand{\\myvector}[1]{\\mymatrix{c}{#1}} $\n",
    "$ \\newcommand{\\myrvector}[1]{\\mymatrix{r}{#1}} $\n",
    "$ \\newcommand{\\mypar}[1]{\\left( #1 \\right)} $\n",
    "$ \\newcommand{\\mybigpar}[1]{ \\Big( #1 \\Big)} $\n",
    "$ \\newcommand{\\sqrttwo}{\\frac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\dsqrttwo}{\\dfrac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\onehalf}{\\frac{1}{2}} $\n",
    "$ \\newcommand{\\donehalf}{\\dfrac{1}{2}} $\n",
    "$ \\newcommand{\\hadamard}{ \\mymatrix{rr}{ \\sqrttwo & \\sqrttwo \\\\ \\sqrttwo & -\\sqrttwo }} $\n",
    "$ \\newcommand{\\vzero}{\\myvector{1\\\\0}} $\n",
    "$ \\newcommand{\\vone}{\\myvector{0\\\\1}} $\n",
    "$ \\newcommand{\\vhadamardzero}{\\myvector{ \\sqrttwo \\\\  \\sqrttwo } } $\n",
    "$ \\newcommand{\\vhadamardone}{ \\myrvector{ \\sqrttwo \\\\ -\\sqrttwo } } $\n",
    "$ \\newcommand{\\myarray}[2]{ \\begin{array}{#1}#2\\end{array}} $\n",
    "$ \\newcommand{\\X}{ \\mymatrix{cc}{0 & 1 \\\\ 1 & 0}  } $\n",
    "$ \\newcommand{\\Z}{ \\mymatrix{rr}{1 & 0 \\\\ 0 & -1}  } $\n",
    "$ \\newcommand{\\Htwo}{ \\mymatrix{rrrr}{ \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} } } $\n",
    "$ \\newcommand{\\CNOT}{ \\mymatrix{cccc}{1 & 0 & 0 & 0 \\\\ 0 & 1 & 0 & 0 \\\\ 0 & 0 & 0 & 1 \\\\ 0 & 0 & 1 & 0} } $\n",
    "$ \\newcommand{\\norm}[1]{ \\left\\lVert #1 \\right\\rVert } $"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2> <font color=\"blue\"> Solutions for </font>Phase Kickback</h2>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"task1\"></a>\n",
    "<h3> Task 1</h3>\n",
    "\n",
    "Create a quantum circuit with two qubits.\n",
    "\n",
    "Set the state of the first qubit to $ \\ket{0} $.\n",
    "\n",
    "Set the state of the second qubit to $ \\ket{1} $.\n",
    "\n",
    "Apply Hadamard to both qubits.\n",
    "\n",
    "Apply CNOT operator, where the controller qubit is the first qubit and the target qubit is the second qubit.\n",
    "\n",
    "Apply Hadamard to both qubits.\n",
    "\n",
    "Measure the outcomes.\n",
    "\n",
    "We start in quantum state $ \\ket{01} $. What is the outcome?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Solution </h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import all necessary objects and methods for quantum circuits\n",
    "from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, execute, Aer\n",
    "from qiskit.tools.visualization import matplotlib_circuit_drawer as drawer\n",
    "\n",
    "qreg1 =  QuantumRegister(2) # quantum register with 2 qubits\n",
    "creg1 = ClassicalRegister(2) # classical register with 2 bits\n",
    "\n",
    "mycircuit1 = QuantumCircuit(qreg1,creg1) # quantum circuit with quantum and classical registers\n",
    "\n",
    "# the first qubit is in |0>\n",
    "\n",
    "# set the second qubit to |1>\n",
    "mycircuit1.x(qreg1[1]) # apply x-gate (NOT operator)\n",
    "\n",
    "# apply Hadamard to both qubits.\n",
    "mycircuit1.h(qreg1[0])\n",
    "mycircuit1.h(qreg1[1])\n",
    "\n",
    "# apply CNOT operator, where the controller qubit is the first qubit and the target qubit is the second qubit.\n",
    "mycircuit1.cx(qreg1[0],qreg1[1])\n",
    "\n",
    "# apply Hadamard to both qubits.\n",
    "mycircuit1.h(qreg1[0])\n",
    "mycircuit1.h(qreg1[1])\n",
    "\n",
    "# measure both qubits\n",
    "mycircuit1.measure(qreg1,creg1)\n",
    "\n",
    "# execute the circuit 100 times in the local simulator\n",
    "\n",
    "job = execute(mycircuit1,Aer.get_backend('qasm_simulator'),shots=100)\n",
    "counts = job.result().get_counts(mycircuit1)\n",
    "\n",
    "# print the reverse of the outcome\n",
    "for outcome in counts:\n",
    "    reverse_outcome = ''\n",
    "    for i in outcome:\n",
    "        reverse_outcome = i + reverse_outcome\n",
    "    print(\"We start in quantum state 01 and\",reverse_outcome,\"is observed\",counts[outcome],\"times\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"task2\"></a>\n",
    "<h3> Task 2 </h3>\n",
    "\n",
    "Create a curcuit with 7 qubits.\n",
    "\n",
    "Set the states of the first six qubits to $ \\ket{0} $.\n",
    "\n",
    "Set the state of the last qubit to $ \\ket{1} $.\n",
    "\n",
    "Apply Hadamard operators to all qubits.\n",
    "\n",
    "Apply CNOT operator (first-qubit,last-qubit) \n",
    "<br>\n",
    "Apply CNOT operator (fourth-qubit,last-qubit)\n",
    "<br>\n",
    "Apply CNOT operator (fifth-qubit,last-qubit)\n",
    "\n",
    "Apply Hadamard operators to all qubits.\n",
    "\n",
    "Measure all qubits. \n",
    "\n",
    "For each CNOT operator, do we have phase-kickback affect?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Solution </h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import all necessary objects and methods for quantum circuits\n",
    "from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, execute, Aer\n",
    "from qiskit.tools.visualization import matplotlib_circuit_drawer as drawer\n",
    "\n",
    "# Create a curcuit with 7 qubits.\n",
    "n = 7\n",
    "qreg2 =  QuantumRegister(n) # quantum register with 7 qubits\n",
    "creg2 = ClassicalRegister(n) # classical register with 7 bits\n",
    "\n",
    "mycircuit2 = QuantumCircuit(qreg2,creg2) # quantum circuit with quantum and classical registers\n",
    "\n",
    "# the first six qubits are already in |0>\n",
    "\n",
    "# set the last qubit to |1>\n",
    "mycircuit2.x(qreg2[n-1]) # apply x-gate (NOT operator)\n",
    "\n",
    "# apply Hadamard to all qubits.\n",
    "for i in range(n):\n",
    "    mycircuit2.h(qreg2[i])\n",
    "\n",
    "\n",
    "# apply CNOT operator (first-qubit,last-qubit) \n",
    "# apply CNOT operator (fourth-qubit,last-qubit) \n",
    "# apply CNOT operator (fifth-qubit,last-qubit)\n",
    "mycircuit2.cx(qreg2[0],qreg2[n-1])\n",
    "mycircuit2.cx(qreg2[3],qreg2[n-1])\n",
    "mycircuit2.cx(qreg2[4],qreg2[n-1])\n",
    "\n",
    "# apply Hadamard to all qubits.\n",
    "for i in range(n):\n",
    "    mycircuit2.h(qreg2[i])\n",
    "\n",
    "# measure all qubits\n",
    "mycircuit2.measure(qreg2,creg2)\n",
    "\n",
    "# execute the circuit 100 times in the local simulator\n",
    "\n",
    "job = execute(mycircuit2,Aer.get_backend('qasm_simulator'),shots=100)\n",
    "counts = job.result().get_counts(mycircuit2)\n",
    "\n",
    "# print the reverse of the outcome\n",
    "for outcome in counts:\n",
    "    reverse_outcome = ''\n",
    "    for i in outcome:\n",
    "        reverse_outcome = i + reverse_outcome\n",
    "    print(reverse_outcome,\"is observed\",counts[outcome],\"times\")\n",
    "    for i in range(len(reverse_outcome)-1):\n",
    "        print(\"the final value of the\",(i+1),\". qubit is\",reverse_outcome[i])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
